package com.woniu.payback_font.web;

import com.github.pagehelper.PageInfo;
import com.woniu.payback_font.entity.Manager;
import com.woniu.payback_font.entity.Permission;
import com.woniu.payback_font.result.ResponseResult;
import com.woniu.payback_font.service.ManagerService;
import com.woniu.payback_font.util.JwtInfo;
import com.woniu.payback_font.util.JwtUtil;

import com.woniu.payback_font.vo.PermissionMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("Manager接口Api")
@RequestMapping("manager")
public class ManagerController {

    @Autowired
    ManagerService managerService;

    @Autowired
    JwtUtil jwtUtil;

    /**
     * 用户账号密码登录接口
     *
     * @param mCode     用户账号
     * @param mPassword 用户原始密码（验证规则 MD5+盐uuid  加密 1235次）
     * @return
     */
    @PostMapping("/byCodeLogin")
    public ResponseResult<String> loginByCode(String mCode, String mPassword) {
        ResponseResult<String> result = null;
        Manager manager = managerService.selectByMCode(mCode);
        //根据数据库存储盐值，以及页面输入密码进行加密对比
        String md5Hash = new Md5Hash(mPassword, manager.getmSaltValue()).toString();
        if (manager == null) {
            result = new ResponseResult<>(201, "用户信息不存在，请尝试重新登录");
        } else if (manager.getmCodePassword().equals(md5Hash)) {//验证通过登录成功

            JwtInfo jwtInfo = new JwtInfo();
            jwtInfo.setId(manager.getmId());
            jwtInfo.setUserCode(manager.getmCode());
            jwtInfo.setUserName(manager.getmName());
            //获取token 并且返回到客户端
            String token = jwtUtil.cerateToken(jwtInfo);

            result = new ResponseResult<>(200, "登录成功");
            result.setData(token);
        } else {
            result = new ResponseResult<>(201, "密码错误，请尝试重新登录");
        }
        return result;
    }


    /**
     * 用户邮箱密码登录接口
     *
     * @param mEmail    用户邮箱
     * @param mPassword 用户原始密码（验证规则 MD5+盐uuid  加密 1235次）
     * @return
     */
    @PostMapping("/byEmailLogin")
    public ResponseResult<String> loginByEmail(String mEmail, String mPassword){
        ResponseResult<String> result = null;

        Manager manager = managerService.selectByEmail(mEmail);
        //根据数据库存储盐值，以及页面输入密码进行加密对比
        String md5Hash = new Md5Hash(mPassword, manager.getmSaltValue()).toString();
        if (manager == null) {
            result = new ResponseResult<>(201, "用户信息不存在，请尝试重新登录");
        } else if (manager.getmCodePassword().equals(md5Hash)) {//验证通过登录成功

            JwtInfo jwtInfo = new JwtInfo();
            jwtInfo.setId(manager.getmId());
            jwtInfo.setUserCode(manager.getmCode());
            jwtInfo.setUserCode(manager.getmName());
            //获取token 并且返回到客户端
            String token = jwtUtil.cerateToken(jwtInfo);

            result = new ResponseResult<>(200, "登录成功");
            result.setData(token);
        } else {
            result = new ResponseResult<>(201, "密码错误，请尝试重新登录");
        }
        return result;
    }




}
